package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.vo.CatalogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/strategy")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @RequestMapping("/input")
    public String input(String id, Model model){
        //编辑时的回显数据key为strategy

        //catalogs 分类
        List<CatalogVO> volislt = strategyCatalogService.queryCatalogVO();
        model.addAttribute("catalogs", volislt);
        //themes 主题
        List<StrategyTheme> themeList = strategyThemeService.list();
        model.addAttribute("themes", themeList);

        if(StringUtils.hasLength(id)) {
            //编辑回显 strategy
            Strategy strategy = strategyService.get(id);
            model.addAttribute("strategy", strategy);
        }
        return "strategy/input";
    }





    @RequestMapping("/testlist")
    public void testlist(){
        List<Strategy> list = strategyService.list();
        list.forEach(System.out::println);
    }

    //带分页的查询
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") StrategyQuery qo, Model model){
        Page<Strategy> page = strategyService.query(qo);
        model.addAttribute("page", page);
        return "strategy/list";
    }



    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Strategy strategy){
        strategyService.saveOrUpdate(strategy);
        return JsonResult.success();
    }


    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(String id){
        strategyService.delete(id);
        return JsonResult.success();
    }



}
